import urls from 'urls.js';
const app = getApp();

function loginFn(callback) {
    // 登录
    var token = wx.getStorageSync('token');
    var userinfo = wx.getStorageSync('userinfo');
    if (!token) {
        console.log('进入登录')

        wx.login({
            success: res => {
              wx.request({
                  // 必需
                  url: urls.apiUrlV2 + 'v2.login/smallapp',
                  data: {
                      code: res.code,
                      fromUid: wx.getStorageSync('fromUid')
                  },
                  method: 'POST',
                  header: {
                      'Content-Type': 'application/json'
                  },
                  success: (res) => {
                    console.log(res);
                      if (res.data.code == 1) {
                          var userinfo = res.data.data;
                          wx.setStorageSync('token', userinfo.token);
                          wx.setStorageSync('userinfo', userinfo);
                          callback && callback(userinfo);
                      }
                  }
              })
            }
        })
    } else {
        callback && callback(userinfo);
    };
    return userinfo;
}

module.exports = loginFn